<?php

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\SapiEmitter;
use Aura\Router\RouterContainer;
use DI\ContainerBuilder;
use Invoker\Invoker;
use function DI\get;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\FirePHPHandler;
use Monolog\Handler\PHPConsoleHandler;

require __DIR__ . '/../vendor/autoload.php';

$app = new app\App();
$response = $app->handle(
    \Zend\Diactoros\ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES)
);









die;
// Create the logger
$logger = new Logger('my_logger');
// Now add some handlers
$logger->pushHandler(new StreamHandler(__DIR__ . '/../my_app.log', Logger::DEBUG));
$logger->pushHandler(new FirePHPHandler());
$logger->pushProcessor(function ($record) {
    $record['extra']['dummy'] = 'Hello world!';
    $record['extra']['l'] = 'jj';

    return $record;
});

// You can now use your logger
$logger->info('My logger is now ready {username}', ['username' => '张三']);





die;

$loader = new Twig\Loader\FilesystemLoader(__DIR__ . '/../templates');
$twig = new Twig\Environment($loader, [
    'cache' => __DIR__ . '/../compilation_cache',
    'debug' => true,
    'auto_reload' => true,
]);

echo $twig->render('index.html', [
    'title' => '使用模板引擎',
    'welcome' => '欢迎使用 Twig 模板引擎',
]);




die;
$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServerRequestInterface::class => get('request'),
    'request' => function () {
        return ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
    },
]);
$builder->useAnnotations(true);
$container = $builder->build();

$routerContainer = new RouterContainer();
$map = $routerContainer->getMap();
(require __DIR__ . '/../routes.php')($map);
$matcher = $routerContainer->getMatcher();
$request = $container->get('request');
$route = $matcher->match($request);
if (!$route) {
    // get the first of the best-available non-matched routes
    $failedRoute = $matcher->getFailedRoute();

    // which matching rule failed?
    switch ($failedRoute->failedRule) {
        case 'Aura\Router\Rule\Allows':
            // 405 METHOD NOT ALLOWED
            // Send the $failedRoute->allows as 'Allow:'
            break;
        case 'Aura\Router\Rule\Accepts':
            // 406 NOT ACCEPTABLE
            break;
        default:
            // 404 NOT FOUND
            break;
    }
    echo "No route found for the request.";
    exit;
}
$attributes = [];
foreach ($route->attributes as $name => $value) {
    if (null !== $value) {
        $attributes[$name] = $value;
        $request = $request->withAttribute($name, $value);
    }
}
$container->set('request', $request);

$handler = $route->handler;
$response = $container->call($handler, $attributes);

$emitter = new SapiEmitter();
$emitter->emit($response);
